﻿/*
 * ChildItemsList
 * 
 * initial implementation : 20.7.2011
 *
 * Copyright (c) Adam Sida, asida@centrum.cz.
 * All rights reserved.
 * 
 * for license see Petra.Gis.License.txt
 */
using System;
using System.Collections.Generic;

namespace Petra.Gis.RenderingServices.PlanarBase.Support
{


  /// <summary>
  /// generic purpose list for legend nodes, renderers,...
  /// in addition to normal list this one supports parent
  /// and Move method.
  /// </summary>
  /// <typeparam name="T"></typeparam>
  public abstract class ChildItemsList<T> : ListPlus<T>, IChildItemsList<T>
  {

    private IList<T> _items;



    protected ChildItemsList()
      :base()
    {
      _items = new List<T>();
    }



    /// <summary>
    /// if list is readonly, external IList can be supplied as constructor param
    /// which works as internal readwrite data storage
    /// </summary>
    /// <param name="storage"></param>
    protected ChildItemsList(IList<T> storage)
      :base(storage)
    { }




    /// <summary>
    /// moves item from its old position to new. Even if this List<T> is read only, elements can be moved"/>
    /// </summary>
    /// <param name="oldIndex">zero based old item position</param>
    /// <param name="newIndex">zero based new item position</param>
    public virtual void Move(int oldIndex, int newIndex)
    {

      if (oldIndex == newIndex)
        return;

      T item = this[oldIndex];
      this.RemoveAt(oldIndex);
      if (newIndex >= (Count - 1))
        this.Add(item);
      else
        this.Insert(newIndex, item);

    }



  }
}
